<template>
    <view class="select_office_two_input_cp">
        <u-input 
            v-model="office" 
            type="select" 
            @click="show = true"
            placeholder="请选择科室"/>
        <u-select
            mode="mutil-column-auto"
            v-model="show" 
            :list="officeList" 
            @confirm="confirm">
        </u-select>
    </view>
</template>

<script>
//科室选择input组件
import {findSectionRoomSuns} from "@/common/api"
export default {
    name:"SelectOfficeInputCp",
    props:{
        value:{
            type:String,
            default:'',
        },
    },
    data() {
        return {
            show: false,
            officeList:[],
        }
    },
    computed:{
        office:{ //科室
            get(){
                return this.value;
            },
            set(value){
                this.$emit('input',value);
            },
        },
    },
    created(){
        this.getData();
    },
    methods:{
        confirm(e){ //确定按钮点击事件
            this.office = e[0].label+e[1].label;
        },
        getData(){ //获取科室列表
            findSectionRoomSuns(
                {},
            ).then(data=>{
                this.officeList = [];
                data.forEach((item,index)=>{
                    //判断数组中添加父级没
                    let state = false; //表示没添加
                    this.officeList.forEach((_item,_index)=>{
                        if(item.sname == _item.label){
                            state = true; //表示添加了
                            _item.children.push({
                                value:_item.children.length,
                                label:item.sonSname,
                            });
                        }
                    });
                    if(!state){ //表示没有添加父级
                        this.officeList.push({
                            value:this.officeList.length,
                            label:item.sname,
                            children:[{
                                value:0,
                                label:item.sonSname,
                            }],
                        });
                    }
                });
            }).catch(()=>{
                return;
            });
        },
    },
}
</script>

<style lang="scss" scoped>
.select_office_two_input_cp {
    text-align: center;
    width: 100%;
    /deep/.u-input{
        width: 100%;
    }
}
</style>
